package com.hilon.foundation.simple;


import com.hilon.foundation.para.ConditionPara;
import com.hilon.foundation.simplest.BaseSimplestServiceImpl;
import com.hilon.foundation.util.ResultBean;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public abstract class BaseSimpleServiceImpl<T extends EntityBaseSimple, V>
        extends BaseSimplestServiceImpl<T, V> {

    public BaseSimpleServiceImpl(EntityManager entityManager) {
        super(entityManager);
    }

    @Transactional
    public ResultBean<String> append(T entity) {
        System.out.println(entity.getName() + entity.getNo());
        String sql = "select count(e.id) as cnt " +
                "from " + entityName + " e " +
                "where e.no=?1 " +
                "and e.isDeleted=false";
        try {
            System.out.println(sql);
            System.out.println("BaseSimpleServiceImpl(简单表)中的append执行");
            Query query = entityManager.createQuery(sql);
            query.setParameter(1, entity.getNo());
            Long e = (Long) query.getSingleResult();
            if (e == 0L) {
                entity.setTimeCreate(new Date());
                entityManager.merge(entity);
                return new ResultBean<>(200, "成功增加记录");
            } else
                return new ResultBean<>(501, "增加记录失败-记录已经存在");

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ResultBean<>(501, "增加记录失败-数据库访问异常");
        }
    }

    @Transactional
    public ResultBean<String> edit(T entity) {
        System.out.println("BaseSimpleServiceImpl(简单表)中的edit执行");
        System.out.println(entity);
        if (!entity.getPersonCheck().isEmpty())
            return new ResultBean<>(501, "保存记录失败-记录已经审核");
        String sql = "select e " +
                "from " + entityName + " e " +
                "where e.id=?1";
        try {
            System.out.println(sql);
            Query query = entityManager.createQuery(sql);
            query.setParameter(1, entity.getId());
            T e = (T) query.getSingleResult();
            if (e != null) {
                entity.setTimeEdit(new Date());
                entityManager.merge(entity);
                return new ResultBean<>(200, "成功保存当前记录");
            } else
                return new ResultBean<>(501, "编辑记录失败-记录不存在");

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ResultBean<>(501, "编辑记录失败-数据库访问异常");
        }
    }

    @Transactional
    public ResultBean<String> check(T entity) {
        System.out.println("BaseSimpleServiceImpl(简单表)中的check执行");
        String sql = "select e " +
                "from " + entityName + " e " +
                "where e.id=?1";
        try {
            Query query = entityManager.createQuery(sql);
            query.setParameter(1, entity.getId());
            T e = (T) query.getSingleResult();
            if (e != null) {
                entity.setTimeCheck(new Date());
                entityManager.merge(entity);
                return new ResultBean<>(200, "成功审核记录");
            } else
                return new ResultBean<>(501, "审核记录失败-记录不存在");

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ResultBean<>(501, "审核记录失败-数据库访问异常");
        }
    }

    @Transactional
    public ResultBean<String> checkCancel(T entity) {
        System.out.println("BaseSimpleServiceImpl(简单表)中的checkCancel执行");
        String sql = "select e " +
                "from " + entityName + " e " +
                "where e.id=?1";
        try {
            Query query = entityManager.createQuery(sql);
            query.setParameter(1, entity.getId());
            T e = (T) query.getSingleResult();
            if (e != null) {
                entity.setPersonCheck("");
                entity.setTimeCheck(null);
                entityManager.merge(entity);
                return new ResultBean<>(200, "成功反审核记录");
            } else
                return new ResultBean<>(501, "反审核记录失败-记录不存在");

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ResultBean<>(501, "反审核记录失败-数据库访问异常");
        }
    }

    @Transactional
    public ResultBean<String> invalid(T entity) {
        String sql = "select e " +
                "from " + entityName + " e " +
                "where e.id=?1";
        try {
            Query query = entityManager.createQuery(sql);
            query.setParameter(1, entity.getId());
            T e = (T) query.getSingleResult();
            if (e != null) {
                e.setTimeInvalid(new Date());
                entityManager.merge(e);
                return new ResultBean<>(200, "成功作废记录");
            } else
                return new ResultBean<>(501, "作废记录失败-记录不存在");

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ResultBean<>(501, "作废记录失败-数据库访问异常");
        }
    }

    public ResultBean<List<V>> findByCondition(ConditionPara conditionPara) {
        String sql = "select e from " + viewName + " e " +
                "where e.isDeleted=false " +
                "and e.timeCreate between ?1 and ?2 " +
                "and e.categoryType like ?3 " +
                "and e.personCreate like ?4 " +
                "and e.personCheck like ?5 " +
                "and e.categoryName like ?6 " +
                "and e.no like ?7 " +
                "and e.categoryTypeDetail like ?8 " +
                "and e.name like ?9 ";
        switch (conditionPara.getCategoryCheck()) {
            case "未审核":
                sql += "and e.personCheck = '' ";
                break;
            case "已审核":
                sql += "and e.personCheck <> '' ";
                break;
            default:
        }
        sql += conditionPara.getAdditionCondition();

        try {
            System.out.println("根据类型/编码等条件执行findByCondition");
            System.out.println(sql);
            Query query = entityManager.createQuery(sql);
            ConditionPara.formatData(conditionPara);
            query.setParameter(1, conditionPara.getDateBegin());
            query.setParameter(2, conditionPara.getDateEnd());
            query.setParameter(3, conditionPara.getCategoryType());
            query.setParameter(4, conditionPara.getPersonCreate());
            query.setParameter(5, conditionPara.getPersonCheck());
            query.setParameter(6, conditionPara.getCategoryName());
            query.setParameter(7, conditionPara.getNo());
            query.setParameter(8, conditionPara.getCategoryTypeDetail());
            query.setParameter(9, conditionPara.getName());

            List<V> list = (List<V>) query.getResultList();
            return new ResultBean<>(list);
        } catch (Exception e) {
            System.out.println(e);
            return new ResultBean<>(501, "findByCondition执行错误");
        }
    }
    public ResultBean<String> canCheckCancelById(Long idPara){
        return new ResultBean<>("可以反审");
    }
//    @Transactional
//    public ResultBean<String> deleteByIds(List<Long> ids) {
//        String idsStr = SqlStrHandle.getIdsStr(ids);
//
//        String sql = "update " +
//                entityName +
//                " e set e.isDeleted=true " +
//                "where e.id in (" + idsStr + ") " +
//                "and (e.personCheck is null or e.personCheck='') ";
//        ;
//        try {
//            Query query = entityManager.createQuery(sql);
//            int i = query.executeUpdate();
//            if (i > 0)
//                return new ResultBean<>(200, "删除成功");
//            else
//                return new ResultBean<>(501, "删除失败,该记录已审核");
//        } catch (Exception e) {
//            System.out.println(e);
//            return new ResultBean<>(501, "删除程序失败");
//        }
//    }
}
